package com.ctrip.ec.api.admin.controller.store;
import com.ctrip.ec.api.admin.common.Encryption;
import com.ctrip.ec.api.admin.common.ValidateException;
import com.ctrip.ec.api.admin.model.query.QueryModel;
import com.ctrip.ec.api.admin.model.store.StoreModel;
import com.ctrip.ec.biz.dao.role.RoleUserDao;
import com.ctrip.ec.biz.dao.user.UserDao;
import com.ctrip.ec.biz.entity.role.RoleUserEntity;
import com.ctrip.ec.biz.entity.role.SysRole;
import com.ctrip.ec.biz.entity.user.UserEntity;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.store.StoreDao;
import com.ctrip.ec.biz.entity.store.StoreEntity;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
@RestController
@RequestMapping("store")
public class StoreController{
    @Autowired
    private StoreDao store;

    @Autowired
    private UserDao user;

    @Autowired
    private RoleUserDao roleUser;

    @GetMapping("list")
    public List<StoreEntity> list() throws SQLException {
        List<StoreEntity> list = store.list();
        return list;
    }

    @PostMapping("list")
    public List<StoreEntity> list(@RequestBody QueryModel model) throws  SQLException{
        PagerSetting setting = model.build();
        List<StoreEntity> list = store.list(setting);
        RequestContext.setResponseHeaderCount(setting.getRecordCount());
        return list;
    }

    @GetMapping("detail/{id}")
    public  StoreModel detail(@PathVariable Long id) throws SQLException {
        StoreEntity entity = store.entity(id);
        StoreModel model = new StoreModel(entity);
        UserEntity userEntity = user.entity(entity.getUserId());
        if(userEntity != null){
            model.setLoginName(userEntity.getLoginName());
            model.setPassword(userEntity.getPassword());
        }
        return model;

    }

    @PostMapping("save")
    public  Long save(@RequestBody StoreModel entity) throws Exception {


        UserEntity userEntity = user.entityByLoginName(entity.getLoginName());

        if(entity.add()) {
            if(Strings.isNullOrEmpty(entity.getLoginName())){
                throw new ValidateException("用户登录名不能为空");
            }
            if(Strings.isNullOrEmpty(entity.getPassword())){
                throw new ValidateException("用户密码不能为空");
            }

            if (userEntity == null) {
                userEntity = new UserEntity();
                userEntity.setLoginName(entity.getLoginName());
                userEntity.setPassword(Encryption.md5(entity.getPassword()));
                userEntity.setCreateTime(new Timestamp(System.currentTimeMillis()));
                user.save(userEntity);

                //加入默认角色
                RoleUserEntity roleUserEntity = new RoleUserEntity();
                roleUserEntity.setUserId(userEntity.getId());
                roleUserEntity.setRoleId(SysRole.store.getId());
                roleUser.save(roleUserEntity);

                entity.setUserId(userEntity.getId());
            } else {
                throw new ValidateException("系统已经存在相同的登录名");

            }
        }else {
            if (!userEntity.getPassword().equals(entity.getPassword())) {
                userEntity.setPassword(Encryption.md5(entity.getPassword()));
                user.save(userEntity);
            }
        }

        store.save(entity);


        //保存用户


        return entity.getId();
    }

    @PostMapping("delete")
    public  void delete(@RequestBody List<Long> ids) throws SQLException {
        store.delete(ids);
    }
    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws  SQLException{
        store.erase(ids);
    }

    @PostMapping("unerase")
    public  void unerase(@RequestBody List<Long> ids) throws  SQLException{
        store.unerase(ids);
    }
}
